﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.IO;
namespace DalMgr
{
    /// <summary>
    /// 单件模式的类，读取ini类
    /// </summary>
    public class IniCfg
    {

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        private string m_CfgPath;
        private static IniCfg UniqueCfg;
        /// <summary>
        /// 默认配置文件名称
        /// </summary>
        private const string DEFAULT_INI_FILE_NAME = "sftCfg.ini";


        /// <summary>
        /// 私有构造函数
        /// </summary>
        private IniCfg()
        {
            SetCfgDefaul();
        }
        /// <summary>
        /// 设置默认配置文件名称
        /// </summary>
        public void SetCfgDefaul()
        {
            m_CfgPath = AppDomain.CurrentDomain.BaseDirectory + DEFAULT_INI_FILE_NAME;//初始化配置文件路径
        }
        
        /// <summary>
        /// 静态获取保存的类，直接调用
        /// </summary>
        /// <returns></returns>
        public static IniCfg GetCfg()
        {
            if (null == UniqueCfg)
            {
                UniqueCfg = new IniCfg();
            }
            return UniqueCfg;
        }
        /// <summary>
        /// 配置文件路径
        /// </summary>
        public string cfgPath
        {
            get
            {
                return m_CfgPath;
            }
            set
            {
                m_CfgPath = value;
            }
        }
        /// <summary>
        /// 写入字符串
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public string ReadStr(string _Section,string _key,string _Def = "")
        {
            string Ret;
            StringBuilder strTmp = new StringBuilder(255);
            GetPrivateProfileString(_Section, _key, _Def, strTmp, 255, m_CfgPath);
            Ret = strTmp.ToString();
            strTmp = null;
            return Ret;
        }
        /// <summary>
        /// 写入字符串
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public bool WriteCfg(string _Section, string _key,string _value)
        {
            long Ret;
            Ret = WritePrivateProfileString(_Section, _key, _value, m_CfgPath);
            return (0 == Ret);
        }
        /// <summary>
        /// 写入BOOL值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public bool WriteCfg(string _Section, string _key, bool _value)
        {
            string strTmp = _value.ToString();
            return WriteCfg(_Section, _key, strTmp.ToUpper());
        }
        /// <summary>
        /// 写入int值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public bool WriteCfg(string _Section, string _key, int _value)
        {
            string strTmp = _value.ToString();
            return WriteCfg(_Section, _key, strTmp.ToUpper());
        }
        /// <summary>
        /// 读取Bool值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public bool ReadBool(string _Section, string _key,bool _def = false)
        {
            string strTmp;
            strTmp = ReadStr(_Section,_key,_def.ToString());
            return ("TRUE" == strTmp.ToUpper());
        }
        /// <summary>
        /// 读取int值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_def"></param>
        /// <returns></returns>
        public int ReadInt(string _Section, string _key,int _def = 0)
        {
            string strTmp;
            int Ret = 0;
            strTmp = ReadStr(_Section, _key,_def.ToString());
            if (Regex.IsMatch(strTmp,@"^-?\d*$"))
            {
                //判断是否为数字，数字才进行转换
                Ret = Convert.ToInt32(strTmp);
            }
            else
            {
                //如果不是数字，则写入日志，包括section和key
                string str = string.Format("配置文件错误 {0} {1}",_Section,_key);
                SftDataMgr.WriteLog(str);
            }
            return Ret;
        }
    }
}
